/**
* @file pit.h
* Programmable Interval Time interface for X86 platform.
* Contains data structures and function for PIT managment:
* to send commands to PIT, to send/read data to/from PIT, init PIT, 
* Inicialization Control Words constants for PIT etc...
*
* Copyrights 2010 Michal Saman, m.saman@designplus.cz
* This source code is release under the Apache License 2.0.
*/
#ifndef _PIT_H
#define _PIT_H

#ifndef ARCH_X86
#error "[pic.h] is platform-independent. Define ARCH_X86."
#endif

#include <stdint.h>

/**
 * PIT tick counter
 */
extern volatile uint32_t	pit_ticks;

/**
* Operation Command Bit mask.
*/
#define PIT_OCW_MASK_BINCOUNT		1		// 00000001
#define PIT_OCW_MASK_MODE			0xE		// 00001110
#define PIT_OCW_MASK_RL				0x30	// 00110000
#define PIT_OCW_MASK_COUNTER		0xC0	// 11000000

/**
* Operational Command Control bits.
*/
// settings binary count mode
#define PIT_OCW_BINCOUNT_BINARY		0		// 0
#define PIT_OCW_BINCOUNT_BCD		1		// 1
// settings for counter mode
#define	PIT_OCW_MODE_TERMINALCOUNT	0		// 0000
#define PIT_OCW_MODE_ONESHOT		0x2		// 0011
#define PIT_OCW_MODE_RATEGEN		0x4		// 0100
#define PIT_OCW_MODE_SQUAREWAVEGEN	0x6		// 0110
#define PIT_OCW_MODE_SOFTWARETRIG	0x8		// 1000
#define PIT_OCW_MODE_HARDWARETRIG	0xA		// 1010
// settings for data transfer
#define PIT_OCW_RL_LATCH			0		// 000000
#define PIT_OCW_RL_LSBONLY			0x10	// 010000
#define PIT_OCW_RL_MSBONLY			0x20	// 100000
#define PIT_OCW_RL_DATA				0x30	// 110000
// settings the actual counter 
#define PIT_OCW_COUNTER_0			0		// 00000000
#define PIT_OCW_COUNTER_1			0x40	// 01000000
#define PIT_OCW_COUNTER_2			0x80	// 10000000

extern void pit_send_copmmand(uint8_t cmd);

extern void pit_send_data(uint16_t data, uint8_t counter);
extern uint32_t pit_read_data(uint8_t counter);

extern uint32_t pit_set_tick_count(uint32_t i);
extern uint32_t pit_get_tick_count();

extern void pit_start_counter(uint32_t freq, uint8_t counter, uint8_t mode);

extern void pit_init() __attribute__ ((cdecl));
extern bool pit_is_inicialized() __attribute__ ((cdecl));
extern void __attribute__ ((cdecl)) pit_irq();
extern void pit_reload_handler(void (*fn) ());

#endif
